﻿using System.ComponentModel;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Xml.Linq;
using org.nutilities.common;
using System;

namespace sample
{
  [ServiceContract(Namespace = "http://www.nutilities.org/sample")]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  public class ItemService
  {
    //private static readonly ILog _logger = LogManager.GetLogger(typeof(AdapterService));
    private ItemProvider _provider = null;

    /// <summary>
    /// Sample Service Constructor
    /// </summary>
    public ItemService()
    {
      _provider = new ItemProvider(ConfigurationManager.AppSettings);
    }

    /// <summary>
    /// Gets the version of the service.
    /// </summary>
    /// <returns>Returns the version as a string.</returns>
    [Description("Gets the version of the service.")]
    [WebGet(UriTemplate = "/version")]
    public VersionInfo GetVersion()
    {
      return _provider.GetVersion();
    }

    /// <summary>
    /// Initializes the service.
    /// </summary>
    /// <returns>Returns a response.</returns>
    [Description("Initializes the service.")]
    [WebGet(UriTemplate = "/init")]
    public Response Initialize()
    {
      return _provider.Initialize();
    }

    /// <summary>
    /// Gets an Item.
    /// </summary>
    /// <returns>Returns an XML list of all items.</returns>
    [Description("Gets an Item.")]
    [WebGet(UriTemplate = "/items")]
    public XElement GetItems()
    {
      return _provider.GetItems();
    }

    /// <summary>
    /// Gets an Item.
    /// </summary>
    /// <returns>Returns an XML of specified item.</returns>
    [Description("Gets an Item.")]
    [WebGet(UriTemplate = "/items/{id}")]
    public XElement GetItem(string id)
    {
      int identifier = 0;
      Int32.TryParse(id, out identifier);
      return _provider.GetItem(identifier);
    }

    /// <summary>
    /// Posts a Request for an Item.
    /// </summary>
    /// <returns>Returns an item.</returns>
    [Description("Posts a Request for an Item.")]
    [WebInvoke(Method = "POST", UriTemplate = "/items")]
    public XElement PostRequest(Request request)
    {
      return _provider.GetRequestedItem(request);
    }

    /// <summary>
    /// Posts an Item.
    /// </summary>
    /// <returns>Returns a response.</returns>
    [Description("Posts an Item.")]
    [WebInvoke(Method="POST", UriTemplate = "/items")]
    public Response PostItem(Item item)
    {
      return _provider.PostItem(item);
    }
  }
}